D:\a\tools.proto\tools.proto\cli\src\args.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use bp3d_protoc::api::core::generator::Params; |
30 | | use clap::{Parser, ValueEnum}; |
31 | | use std::ffi::OsString; |
32 | | use std::path::{Path, PathBuf}; |
33 | | |
34 | | #[derive(ValueEnum, Copy, Clone, Debug)] |
35 | | pub enum Generator { |
36 | | /// The Rust code generator. |
37 | | Rust, |
38 | | |
39 | | /// The Swift code generator. |
40 | | Swift, |
41 | | } |
42 | | |
43 | | #[derive(ValueEnum, Copy, Clone, Debug)] |
44 | | pub enum Feature { |
45 | | /// The generated code should be able to write messages. |
46 | | WriteMessages, |
47 | | |
48 | | /// The generated code should be able to read messages. |
49 | | ReadMessages, |
50 | | |
51 | | /// The generated code needs enums. |
52 | | UseEnums, |
53 | | |
54 | | /// The generated code needs structures. |
55 | | UseStructs, |
56 | | |
57 | | /// The generated code needs messages. |
58 | | UseMessages, |
59 | | |
60 | | /// The generated code needs unions. |
61 | | UseUnions, |
62 | | } |
63 | | |
64 | | impl Feature { |
65 | 0 | pub fn apply(&self, params: &mut Params) { |
66 | 0 | match self { |
67 | 0 | Feature::WriteMessages => params.write_messages = true, |
68 | 0 | Feature::ReadMessages => params.read_messages = true, |
69 | 0 | Feature::UseEnums => params.use_enums = true, |
70 | 0 | Feature::UseStructs => params.use_structs = true, |
71 | 0 | Feature::UseMessages => params.use_messages = true, |
72 | 0 | Feature::UseUnions => params.use_unions = true, |
73 | | }; |
74 | 0 | } |
75 | | } |
76 | | |
77 | | #[derive(Parser, Debug)] |
78 | | #[command(version, about, long_about = None)] |
79 | | /// CLI tool to call BP3D protocol compiler outside build-scripts and access to source code. |
80 | | pub struct Args { |
81 | | /// List of imported protocols, each by pairs of path to the protocol description and import |
82 | | /// path. |
83 | | #[clap(short = 'i', long = "import", number_of_values = 2)] |
84 | 0 | pub imports: Vec<OsString>, |
85 | | /// List of input protocol description files to compile. |
86 | | #[clap(required=true, num_args=1..)] |
87 | 0 | pub inputs: Vec<PathBuf>, |
88 | | /// Output directory where to place the generated protocols (an additional directory is created |
89 | | /// for each protocol to be compiled). |
90 | | #[clap(short = 'o', long = "output")] |
91 | | pub output: Option<PathBuf>, |
92 | | /// Name of the code generator to use. |
93 | | #[clap(short = 'g', long = "generator", default_value = "rust")] |
94 | 0 | pub generator: Generator, |
95 | | /// Features to enable, the default is to use the default set of features which includes |
96 | | /// everything except reading and writing messages. |
97 | | #[clap(short = 'f', long = "feature")] |
98 | 0 | pub features: Option<Vec<Feature>>, |
99 | | /// The file header to include at the top of each generated file, each line in the file header |
100 | | /// is already formatted according to the line comments syntax of the chosen target generation |
101 | | /// language. |
102 | | #[clap(long = "header")] |
103 | | pub file_header: Option<PathBuf>, |
104 | | } |
105 | | |
106 | | impl Args { |
107 | 0 | pub fn iter_imports(&self) -> impl Iterator<Item = (&Path, &str)> { |
108 | 0 | self.imports.chunks(2).map(|v| (Path::new(&v[0]), v[1].to_str().unwrap())) |
109 | 0 | } |
110 | | } |